#include <olectl.h>
#include "IPEditUI.h"
#define SetadAll(Code) {for(int i = 0; i < 7; ++i){if ( GetItemAt(i)){if (i%2 == 0){((COwnerUI*)GetItemAt(i))->Code;}else{((CLabelUI*)GetItemAt(i))->Code;}}}}


class CEnterWnd : public CWindowWnd
{

public:
	CEnterWnd();
	void Init(COwnerUI* pOwner ,int nIndex);


	RECT CalPos(RECT rcPos);
	LPCTSTR GetWindowClassName() const;
	LPCTSTR GetSuperClassName() const;
	void OnFinalMessage(HWND hWnd);
	
	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
	LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnEditChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	void ToNext(bool bReverse = false);
	RECT GetItemPosByIndex(RECT rcOwner,int nIndex);
private:
	COwnerUI* m_pOwner;
	HBRUSH m_hBkBrush;
	bool m_bInit;

	bool m_bInRange;
	int m_nIndex;
	CDuiString m_sNormalImage;
};

CEnterWnd::CEnterWnd() :m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false) ,m_bInRange(true),m_nIndex(-1)
{
	
}

void CEnterWnd::Init(COwnerUI* pOwner ,int nIndex)
{
	m_pOwner = pOwner;
	m_nIndex = nIndex ;
	RECT rcPos = CalPos(pOwner->m_rcEdit[nIndex]);//CalPos();
	UINT uStyle = WS_CHILD | ES_AUTOHSCROLL|ES_CENTER  ;
	Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
	HFONT hFont=NULL;
	int iFontIndex=m_pOwner->GetFont();
	if (iFontIndex!=-1)
	{
		hFont=m_pOwner->GetManager()->GetFont(iFontIndex);
	}
	if (hFont==NULL)
	{
		hFont=m_pOwner->GetManager()->GetDefaultFontInfo()->hFont;
	}

	SetWindowFont(m_hWnd, hFont, TRUE);
	Edit_LimitText(m_hWnd, 3);//m_pOwner->GetMaxChar());

	Edit_SetText(m_hWnd, m_pOwner->m_sDisplay[nIndex]);
	Edit_SetModify(m_hWnd, FALSE);
	SendMessage(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELPARAM(0, 0));
	Edit_Enable(m_hWnd, m_pOwner->IsEnabled() == true);
	//Styls
	LONG styleValue = ::GetWindowLong(m_hWnd, GWL_STYLE);
	styleValue |= pOwner->GetWindowStyls();
	::SetWindowLong(GetHWND(), GWL_STYLE, styleValue);
	::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
	::SetFocus(m_hWnd);
	m_bInit = true;    

}

RECT CEnterWnd::CalPos(RECT rcPos)
{
	RECT rcInset = m_pOwner->GetTextPadding();

	LONG lEditHeight = m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())->tm.tmHeight;
	if( lEditHeight < (rcPos.bottom - rcPos.top) ) 
	{
		rcPos.top += ((rcPos.bottom - rcPos.top) - lEditHeight) / 2;
		rcPos.bottom = rcPos.top + lEditHeight;
	}
	return rcPos;
}

LPCTSTR CEnterWnd::GetWindowClassName() const
{
	return _T("IPEditWnd");
}

LPCTSTR CEnterWnd::GetSuperClassName() const
{
	return WC_EDIT;
}

void CEnterWnd::OnFinalMessage(HWND /*hWnd*/)
{
	m_pOwner->Invalidate();
	if( m_hBkBrush != NULL ) 
	{
		::DeleteObject(m_hBkBrush);
	}
	m_pOwner->m_pEditWnd[m_nIndex] = NULL;
	delete this;
}

LRESULT CEnterWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;
	BOOL bHandled = TRUE;
	if( uMsg == WM_KILLFOCUS ) 
	{
		lRes = OnKillFocus(uMsg, wParam, lParam, bHandled);
	}
	else if( uMsg == OCM_COMMAND ) 
	{
		if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE ) 
		{
			lRes = OnEditChanged(uMsg, wParam, lParam, bHandled);
		}
		else if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_UPDATE ) 
		{
			RECT rcClient;
			::GetClientRect(m_hWnd, &rcClient);
			::InvalidateRect(m_hWnd, &rcClient, FALSE);
		}
		
	}
	else if( uMsg == WM_KEYDOWN )
	{
		if (TCHAR(wParam) == VK_RETURN ) 
		{
			m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_RETURN);
		}
		//else if (a)
		//{
		//	
		//	if (wParam )
		//	{
		//		bHandled = FALSE;
		//	}
		//	//TCHAR szText[64];
		//	//_stprintf_s(szText,sizeof(szText)/sizeof(TCHAR),_T("%u"),lParam);
		//	//int nLen =Edit_GetTextLength(m_hWnd);
		//	//TCHAR * szText = new TCHAR[nLen + 1];
		//	//Edit_GetText(m_hWnd,szText,nLen + 1);
		//	//if (nLen > 0 && wParam == 'A')
		//	//{

		//	//	szText[nLen - 1] = 0;
		//	//}
		//	//Edit_SetText(m_hWnd ,szText);
		//	//Edit_SetSel(m_hWnd,nLen,nLen);
		//	//delete [] szText;
		//	//MessageBox(m_hWnd,szText ,_T(""),MB_OK);
		//}
		////else if (wParam == VK_BACK)
		////{
		////	bHandled = FALSE;
		////}
		else if(wParam == VK_LEFT || wParam == VK_UP)
		{
			if (LOWORD(Edit_GetSel(m_hWnd)) == 0 && HIWORD(Edit_GetSel(m_hWnd)) == 0 ) 
			{
				ToNext(true);
			}
			else
			{
				bHandled = FALSE;
			}
		}
		else if(wParam == VK_RIGHT || wParam == VK_DOWN)
		{
			if (LOWORD(Edit_GetSel(m_hWnd)) == Edit_GetTextLength(m_hWnd))
			{
				ToNext();
			}
			else
			{
				bHandled = FALSE;
			}
		}
		else if(wParam == VK_DELETE)
		{
			bHandled = FALSE;
		}
	}
	else if( uMsg == OCM__BASE + WM_CTLCOLOREDIT  || uMsg == OCM__BASE + WM_CTLCOLORSTATIC ) 
	{
		if( m_pOwner->m_dwBackColor == 0xFFFFFFFF ) 
		{
			//return NULL;
		}
		::SetBkMode((HDC)wParam, TRANSPARENT);
		DWORD dwTextColor = m_pOwner->GetTextColor();
		::SetTextColor((HDC)wParam, RGB(GetBValue(dwTextColor),GetGValue(dwTextColor),GetRValue(dwTextColor)));
		if( m_hBkBrush == NULL ) 
		{
			DWORD clrColor = m_pOwner->m_dwBackColor;
			m_hBkBrush = ::CreateSolidBrush(RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
		}
		//return (LRESULT)HBRUSH(GetStockObject(HOLLOW_BRUSH));

		return (LRESULT)m_hBkBrush;
	}
	else if (uMsg == WM_CHAR)
	{
		OnChar(uMsg ,wParam ,lParam ,bHandled);
	}
	else if (uMsg == WM_MOUSEMOVE)
	{
		if (m_pOwner->m_bFirst)
		{
			m_pOwner->m_bFirst = false;
			m_pOwner->m_sOneImage = m_pOwner->GetNormalImage();
			m_pOwner->SetNormalImage(m_pOwner->GetHotImage());
			SetTimer(m_pOwner->GetManager()->GetPaintWindow(),1000,100,&COwnerUI::Timer);
		}
		bHandled = false;
	}
	else
	{
		bHandled = FALSE;
	}
	if( !bHandled ) 
	{
		return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	}
	return lRes;
}

LRESULT CEnterWnd::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
	PostMessage(WM_CLOSE);
	return lRes;
}

LRESULT CEnterWnd::OnEditChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if( !m_bInit ) 
	{
		return 0;
	}
	if( m_pOwner == NULL ) 
	{
		return 0;
	}
	// Copy text back
	int cchLen = ::GetWindowTextLength(m_hWnd) + 1;
	LPTSTR pstr = static_cast<LPTSTR>(_alloca(cchLen * sizeof(TCHAR)));
	ASSERT(pstr);
	if( pstr == NULL ) return 0;
	::GetWindowText(m_hWnd, pstr, cchLen);
	m_pOwner->m_sDisplay[m_nIndex] = pstr;
	m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_TEXTCHANGED);
	return 0;
}

RECT CEnterWnd::GetItemPosByIndex(RECT rcOwner,int nIndex)
{
	int nWidth = rcOwner.right - rcOwner.left;
	int nHeight = rcOwner.bottom - rcOwner.top ;
	int nItem = (nWidth - 5 *3 )/4;
	RECT rcPos={0};
	rcPos.left = nItem*nIndex + 5 *nIndex +rcOwner.left;
	rcPos.top = rcOwner.top;
	rcPos.right = rcPos.left + nItem;
	rcPos.bottom = rcOwner.bottom;
	return rcPos;
}

void CEnterWnd::ToNext( bool bReverse)
{
	if (bReverse)
	{
		if(m_nIndex > 0)
		{
			m_pOwner->NextEditWnd(m_nIndex-1);
			TCHAR szWord[64];
			TCHAR* szText = szWord+5;
			Edit_GetText(m_pOwner->m_pEditWnd[m_nIndex - 1]->GetHWND(),szText ,59);
			int nLen = _tcslen(szText) ;
			//szText[nLen] = 0;
			//m_pOwner->m_pFrontEdit->SetText(szText);
			Edit_SetSel(m_pOwner->m_pEditWnd[m_nIndex - 1]->GetHWND(),nLen,nLen);
		}
	}
	else
	{
		if (m_nIndex < 3)
		{
			m_pOwner->NextEditWnd(m_nIndex + 1);
			Edit_SetSel(m_pOwner->m_pEditWnd[m_nIndex + 1]->GetHWND(),0,-1);
		}
	}
}

LRESULT CEnterWnd::OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if (! ((wParam >= '0'&& wParam <= '9') ||wParam == '.'|| wParam ==VK_BACK ||wParam == VK_LEFT ||wParam == VK_RIGHT || wParam == VK_UP || wParam == VK_DOWN) )
	{
		return 0;	
	}
	if (wParam != '.')
	{
		bHandled = FALSE;
	}
	if (wParam == '.')
	{
		if( LOWORD(Edit_GetSel(m_hWnd)) != 0)  
		{
			ToNext();
		}
	}
	else if (wParam == VK_BACK)
	{
		if (LOWORD(Edit_GetSel(m_hWnd)) == 0 )
		{
			ToNext(true);
		}
	}

	else if (Edit_GetTextLength(m_hWnd) > 1)// && !(HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd)))  )
	{
		TCHAR szText[64];
		Edit_GetText(m_hWnd,szText,sizeof(szText)/sizeof(TCHAR));
		int nSel = Edit_GetSel(m_hWnd) ;
		int nEnter = Edit_GetTextLength(m_hWnd);
		if (Edit_GetTextLength(m_hWnd) < 3 && !HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd)) )
		{
			for (int i = _tcslen(szText) ; i >= LOWORD(nSel) ; --i)
			{
				szText[i + 1] = szText[i];
			}
			szText[LOWORD(nSel)] = wParam;
		}
		if (HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd)))
		{
			szText[LOWORD(nSel)] = wParam;
			for (int i = LOWORD(nSel) ,nLen = _tcslen(szText) ; i < nLen ; ++i)
			{
				szText[i + 1] = szText[HIWORD(nSel) - LOWORD(nSel) + i ];
			}
		}
		if (_tstoi(szText) >= 255 )
		{
			Edit_SetText(m_hWnd ,_T("255"));
			m_bInRange = false;
		}
		else 
		{
			m_bInRange = true;
			if (Edit_GetTextLength(m_hWnd)== 2&&LOWORD(Edit_GetSel(m_hWnd)) == 2 || (Edit_GetTextLength(m_hWnd)== 3&&LOWORD(Edit_GetSel(m_hWnd)) == 3))
			{
				ToNext();
			}
		}
	}
	return 0;
}

//***********************************************************************************
//
// COwnerUI 
//***********************************************************************************

COwnerUI* COwnerUI::m_pCurrent = NULL;
COwnerUI::COwnerUI() :
	 m_pWindow(NULL)
	,m_dwTextColor(0xFF000000)
	,m_dwDotColor(0)
	,m_uButtonState(0) 
	,m_dwEditbkColor(0xFFFFFFFF)
	,m_iWindowStyls(0)
	,m_nIndex(-1)
	,m_nDotFont(-1)
	,m_bFirst(true)
{
	SetTextPadding(CDuiRect(4, 3, 4, 3));
	SetBkColor(0xFFFFFFFF);
	SetTextStyle(5);

	for (int i = 0 ; i < 4 ; ++i)
	{
		m_pEditWnd[i] = NULL;
	}
	
	m_pCurrent = this;

}

UINT COwnerUI::GetControlFlags() const
{
	if( !IsEnabled() ) 
	{
		return CControlUI::GetControlFlags();
	}
	return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
}

LPCTSTR COwnerUI::GetClass() const
{
	return _T("IPEditUI");
}

void COwnerUI::DoEvent(TEventUI& event)
{
	if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) 
	{
		if( m_pParent != NULL ) m_pParent->DoEvent(event);
		else CLabelUI::DoEvent(event);
		return;
	}
	if( event.Type == UIEVENT_SETCURSOR && IsEnabled() )
	{
		::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
		return;
	}
	if( event.Type == UIEVENT_WINDOWSIZE )
	{
		if( GetFull() != NULL ) m_pManager->SetFocusNeeded(this);
	}
	if( event.Type == UIEVENT_SCROLLWHEEL )
	{
		if( GetFull() != NULL ) return;
	}
	if( event.Type == UIEVENT_SETFOCUS && IsEnabled() ) 
	{
		m_bFocused = true;
		SetTextColor(m_dwTextColor);
		int m_nIndex = GetIndexByPos(event.ptMouse);
		if (m_nIndex == -1)
		{
			return ;
		}
		if (m_pEditWnd[m_nIndex] )
		{
			return ;
		}
		m_pEditWnd[m_nIndex]  = new CEnterWnd;
		ASSERT(m_pEditWnd[m_nIndex]);
		m_pEditWnd[m_nIndex]->Init(this ,m_nIndex);
		Invalidate();
	}
	if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() ) 
	{
		Invalidate();
	}
	if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN) 
	{
		m_nIndex = GetIndexByPos(event.ptMouse);
		if (m_nIndex == -1)
		{
			return ;
		}
		if( IsEnabled() ) 
		{
			GetManager()->ReleaseCapture();
			if( IsFocused() && m_pEditWnd[m_nIndex] == NULL )
			{
				m_pEditWnd[m_nIndex] = new CEnterWnd();
				ASSERT(m_pEditWnd[m_nIndex]);
				m_pEditWnd[m_nIndex]->Init(this,m_nIndex);
				if( PtInRect(&m_rcItem, event.ptMouse) )
				{
					int nSize = GetWindowTextLength(*(m_pEditWnd[m_nIndex]));
					if( nSize == 0 )
						nSize = 1;

					Edit_SetSel(*(m_pEditWnd[m_nIndex]), 0, nSize);
				}
			}
			else if( m_pEditWnd[m_nIndex] != NULL )
			{
#if 1			
				int nSize = GetWindowTextLength(*(m_pEditWnd[m_nIndex]));
				if( nSize == 0 )
					nSize = 1;

				Edit_SetSel(*(m_pEditWnd[m_nIndex]), 0, nSize);
#else
				POINT pt = event.ptMouse;
				pt.x -= m_rcItem.left + m_rcTextPadding.left;
				pt.y -= m_rcItem.top + m_rcTextPadding.top;
				::SendMessage(*(m_pEditWnd[m_nIndex]), WM_LBUTTONDOWN, event.wParam, MAKELPARAM(pt.x, pt.y));
#endif
			}
		}
		return;
	}
	if( event.Type == UIEVENT_MOUSEMOVE ) 
	{
		return;
	}
	if( event.Type == UIEVENT_BUTTONUP ) 
	{
		return;
	}
	if( event.Type == UIEVENT_CONTEXTMENU )
	{
		return;
	}
	if( event.Type == UIEVENT_MOUSEENTER )
	{
		if( IsEnabled() && m_bFirst) 
		{
			m_bFirst = false ;
			m_sOneImage = GetNormalImage();
			SetNormalImage(GetHotImage());
			//m_uButtonState |= UISTATE_HOT;
			Invalidate();
			SetTimer(GetManager()->GetPaintWindow(),1000,100,&COwnerUI::Timer);
		}
		return;
	}
	if( event.Type == UIEVENT_MOUSELEAVE )
	{
		if( IsEnabled()) 
		{
			//m_uButtonState &= ~UISTATE_HOT;
			//Invalidate();
		}
		return;
	}
	CLabelUI::DoEvent(event);
}

void COwnerUI::SetEnabled(bool bEnable)
{
	CControlUI::SetEnabled(bEnable);
	if( !IsEnabled() ) 
	{
		m_uButtonState = 0;
	}
}

void COwnerUI::SetText(LPCTSTR pstrText ,int nIndex)
{
	if (nIndex < 0 || nIndex >= 4) 
	{
		return ;
	}
	m_sDisplay[nIndex] = pstrText;
	if( m_pEditWnd[nIndex] != NULL ) 
	{
		Edit_SetText(*m_pWindow, m_sText);
	}
	Invalidate();
}


int COwnerUI::GetWindowStyls() const 
{
	return m_iWindowStyls;
}

LPCTSTR COwnerUI::GetNormalImage()
{
	return m_sNormalImage;
}

void COwnerUI::SetNormalImage(LPCTSTR pStrImage)
{
	m_sNormalImage = pStrImage;
	Invalidate();
}

LPCTSTR COwnerUI::GetHotImage()
{
	return m_sHotImage;
}

void COwnerUI::SetHotImage(LPCTSTR pStrImage)
{
	m_sHotImage = pStrImage;
	Invalidate();
}

LPCTSTR COwnerUI::GetFocusedImage()
{
	return m_sFocusedImage;
}

void COwnerUI::SetFocusedImage(LPCTSTR pStrImage)
{
	m_sFocusedImage = pStrImage;
	Invalidate();
}

LPCTSTR COwnerUI::GetDisabledImage()
{
	return m_sDisabledImage;
}

void COwnerUI::SetDisabledImage(LPCTSTR pStrImage)
{
	m_sDisabledImage = pStrImage;
	Invalidate();
}

void COwnerUI::SetNativeEditBkColor(DWORD dwBkColor)
{
	m_dwEditbkColor = dwBkColor;
}

DWORD COwnerUI::GetNativeEditBkColor() const
{
	return m_dwEditbkColor;
}

void COwnerUI::SetSel(long nStartChar, long nEndChar)
{
	if( m_pWindow != NULL ) Edit_SetSel(*m_pWindow, nStartChar,nEndChar);
}

void COwnerUI::SetSelAll()
{
	SetSel(0,-1);
}

void COwnerUI::SetReplaceSel(LPCTSTR lpszReplace)
{
	if( m_pWindow != NULL ) Edit_ReplaceSel(*m_pWindow, lpszReplace);
}

void COwnerUI::SetPos(RECT rc)
{
	CControlUI::SetPos(rc);
	for (int i = 0; i < 4;++i)
	{
		if( m_pEditWnd[i] != NULL ) 
		{
			RECT rcPos = m_pEditWnd[i]->CalPos(m_rcEdit[i]);
			::SetWindowPos(m_pEditWnd[i]->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left, 
				rcPos.bottom - rcPos.top, SWP_NOZORDER | SWP_NOACTIVATE);        
		}
	}
}

void COwnerUI::SetVisible(bool bVisible)
{
	CControlUI::SetVisible(bVisible);
	if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
}

void COwnerUI::SetInternVisible(bool bVisible)
{
	if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
}

SIZE COwnerUI::EstimateSize(SIZE szAvailable)
{
	if( m_cxyFixed.cy == 0 ) 
	{
		return CSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 6);
	}
	return CControlUI::EstimateSize(szAvailable);
}

void COwnerUI::PaintStatusImage(HDC hDC )
{
	m_dwDotColor = m_dwDotColor == 0 ?m_dwTextColor :m_dwDotColor;
	m_nDotFont = m_nDotFont == -1 ?m_iFont :m_nDotFont;
	for(int i = 0;i < 3 ;++i)
	{
		RECT rcEdit = GetPosByIndex(i);

		rcEdit.left = rcEdit.right  ;
		rcEdit.right = rcEdit.left + DotWidth ;
		LONG lEditHeight = GetManager()->GetFontInfo(GetFont())->tm.tmHeight;
		
		if( lEditHeight < (rcEdit.bottom - rcEdit.top) ) 
		{
			rcEdit.top += ((rcEdit.bottom - rcEdit.top) - lEditHeight) / 2;
			rcEdit.bottom = rcEdit.top + lEditHeight;
		}
		//int x = rcEdit.left + DotWidth/2 - 1;
		//int y = rcEdit.bottom - lEditHeight/5 ;
		//DWORD dwColor =RGB(GetBValue(m_dwDotColor),GetGValue(m_dwDotColor),GetRValue(m_dwDotColor));
		//SetPixel(hDC ,x,y,dwColor);SetPixel(hDC ,x+1,y,dwColor);SetPixel(hDC ,x,y+1,dwColor);SetPixel(hDC ,x+1,y+1,dwColor);
		CRenderEngine::DrawText(hDC, m_pManager , rcEdit, _T("."), m_dwDotColor,m_nDotFont, DT_SINGLELINE | m_uTextStyle);
	}
	if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
	else m_uButtonState &= ~ UISTATE_FOCUSED;
	if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
	else m_uButtonState &= ~ UISTATE_DISABLED;

	if( (m_uButtonState & UISTATE_DISABLED) != 0 ) 
	{
		if( !m_sDisabledImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sDisabledImage) ) m_sDisabledImage.Empty();
			else return;
		}
	}
	else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) 
	{
		if( !m_sFocusedImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sFocusedImage) ) m_sFocusedImage.Empty();
			else return;
		}
	}
	else if( (m_uButtonState & UISTATE_HOT) != 0 ) 
	{
		if( !m_sHotImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sHotImage) ) m_sHotImage.Empty();
			else return;
		}
	}
	if( !m_sNormalImage.IsEmpty() ) 
	{
		if( !DrawImage(hDC, (LPCTSTR)m_sNormalImage) ) m_sNormalImage.Empty();
		else return;
	}
}

void COwnerUI::PaintText(HDC hDC)
{
	if( m_dwTextColor == 0 ) 
	{
		m_dwTextColor = m_pManager->GetDefaultFontColor();
	}
	if( m_dwDisabledTextColor == 0 ) 
	{
		m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
	}
	for (int i = 0; i < 4 ;++i)
	{
		CDuiString sText = m_sDisplay[i];

		if( m_sDisplay[i].IsEmpty() ) 
		{
			continue;
		}
		RECT rc = m_rcEdit[i];
		if( IsEnabled() )
		{
			CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor,m_iFont, DT_SINGLELINE | m_uTextStyle);
		}
		else 
		{
			CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor,m_iFont, DT_SINGLELINE | m_uTextStyle);
		}
	}
}

CEnterWnd* COwnerUI::GetFull()
{
	for (int i = 0; i < 4; ++i)
	{
		if (m_pEditWnd[i])
		{
			return m_pEditWnd[i];
		}
	}
	return NULL;
}

void COwnerUI::SetTextColor(DWORD dwColor)
{
	m_dwTextColor = dwColor;
	CLabelUI::SetTextColor(dwColor);
	if (m_pWindow)
	{
		HDC hDC = GetDC(m_pWindow->GetHWND());
		::SetTextColor(hDC,dwColor);
		ReleaseDC(m_pWindow->GetHWND() ,hDC);
	}
	
}

int COwnerUI::GetIndexByPos(POINT ptMouse)
{
	ptMouse = m_pManager->GetMousePos();
	RECT rcBox = CLabelUI::GetPos();
	RECT rcPanel;
	rcPanel.left = rcBox.left + m_rcPadding.left;
	rcPanel.right = rcBox.right - m_rcPadding.right ;
	rcPanel.top = rcBox.top + m_rcPadding.top ;
	rcPanel.bottom = rcBox.bottom - m_rcPadding.bottom;
	int nWidth = rcPanel.right - rcPanel.left ;
	int nItem = 0;
	int nMore = 0;
	if (nWidth > 0)
	{
		nItem = (nWidth - DotWidth*3)/4 ;
		nMore = (nWidth - DotWidth*3)%4 ;
	}

	for (int i = 0 ;i < 4 ;++i )
	{
		m_rcEdit[i].left = rcPanel.left + i * (nItem + (i<=nMore?1:0)) + (i>nMore?nMore:0) + i * DotWidth  ;
		m_rcEdit[i].right = m_rcEdit[i].left + nItem + (i<nMore?1:0);
		m_rcEdit[i].top = rcPanel.top ;
		m_rcEdit[i].bottom = rcPanel.bottom ;
		if (ptMouse.x >=  m_rcEdit[i].left && ptMouse.x <  m_rcEdit[i].right )
		{
			return i;
		}
	}
	return -1;
}

RECT COwnerUI::GetPosByIndex(int nIndex)
{
	if (nIndex < 0 || nIndex > 3) return RECT();
	RECT rcBox = CLabelUI::GetPos();
	RECT rcPanel;
	rcPanel.left = rcBox.left + m_rcPadding.left;
	rcPanel.right = rcBox.right - m_rcPadding.right ;
	rcPanel.top = rcBox.top + m_rcPadding.top ;
	rcPanel.bottom = rcBox.bottom - m_rcPadding.bottom;
	int nWidth = rcPanel.right - rcPanel.left;
	int nItem = 0;
	int nMore = 0;
	if (nWidth > 0)
	{
		nItem = (nWidth - DotWidth*3)/4 ;
		nMore = (nWidth - DotWidth*3)%4;
	}
	m_rcEdit[nIndex].left = rcPanel.left + nIndex * (nItem + (nIndex<=nMore?1:0)) + (nIndex>nMore?nMore:0) + nIndex * DotWidth ;
	m_rcEdit[nIndex].right = m_rcEdit[nIndex].left + nItem + (nIndex<nMore?1:0);
	m_rcEdit[nIndex].top = rcPanel.top;
	m_rcEdit[nIndex].bottom = rcPanel.bottom;
	return m_rcEdit[nIndex];
}

void COwnerUI::NextEditWnd(int nIndex)
{
	GetPosByIndex(nIndex);
	if (!m_pEditWnd[nIndex])
	{
		m_pEditWnd[nIndex] = new CEnterWnd;
		m_pEditWnd[nIndex]->Init(this ,nIndex);
	}
	::SetFocus(m_pEditWnd[nIndex]->GetHWND());
}

void COwnerUI::SetDotColor(DWORD dwColor)
{
	m_dwDotColor = dwColor;
}

void COwnerUI::SetDotFont(int nIndex)
{
	m_nDotFont = nIndex;
}

UINT COwnerUI::GetIP()
{
	UINT nIP = 0;
	for (int i = 0; i < 4; ++i)
	{
		if (i) 
		{
			nIP <<= 8;
		}
		*((BYTE*)&nIP ) |= _tcstoul(m_sDisplay[i] ,NULL,10);
	}
	return nIP;
}

void COwnerUI::SetIP(UINT nIP)
{
	for (int i = 0; i < 4; ++i)
	{
		m_sDisplay[i].Format(_T("%u") ,nIP>>( (3 - i) * 8) & 0xFF );
	}
	GetPosByIndex(3);
	Invalidate();
}

void  COwnerUI::Timer(HWND hwnd,UINT uMsg, UINT_PTR idEvent,DWORD dwTime)
{
	m_pCurrent->IsInRect( hwnd, uMsg,  idEvent, dwTime);
}

void COwnerUI::IsInRect(HWND hwnd,UINT uMsg, UINT_PTR idEvent,DWORD dwTime)
{
	RECT rcPos = CLabelUI::GetPos();
	POINT ptMouse;
	GetCursorPos(&ptMouse);
	ScreenToClient(GetManager()->GetPaintWindow(),&ptMouse);
	if ( !PtInRect(&rcPos,ptMouse))
	{
		KillTimer(GetManager()->GetPaintWindow() ,idEvent);			
		SetNormalImage(m_sOneImage);
		m_bFirst = true;
	}
}